package com.tgy.MyFlowable.workflow.controller;

import com.ctsi.core.auth.annotation.PreAuth;
import com.ctsi.core.common.api.Result;
import com.ctsi.core.common.util.$;
import com.ctsi.core.database.entity.Search;
import com.ctsi.core.web.controller.BaseController;
import com.ctsi.workflow.dto.FlwFormDTO;
import com.ctsi.workflow.dto.FlwFormResourceDTO;
import com.ctsi.workflow.entity.FlwForm;
import com.ctsi.workflow.service.IFlwFormResourceService;
import com.ctsi.workflow.service.IFlwFormService;
import com.ctsi.workflow.vo.FlwFormVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 流程表单配置表 前端控制器
 *
 * @version 1.0
 * @author: wang xiao xiang
 * @date: 2021/8/8 20:14
 */
@RestController
@AllArgsConstructor
@RequestMapping("/flow/form")
@Api(tags = "工作流：流程表单")
public class FlwFormController extends BaseController {

    private final IFlwFormService flwFormService;

    private final IFlwFormResourceService flwFormResourceService;

    /**
     * 分页列表
     *
     * @param search 　搜索关键词
     * @return Result
     */
    @PreAuth
    @GetMapping("/page")
    @ApiOperation(value = "分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", required = true, value = "当前页", paramType = "form"),
            @ApiImplicitParam(name = "size", required = true, value = "每页显示数据", paramType = "form"),
            @ApiImplicitParam(name = "keyword", required = true, value = "模糊查询关键词", paramType = "form"),
    })
    public Result<?> page(Search search) {
        return Result.data(flwFormService.listPage(search));
    }

    /**
     * 流程表单配置表信息
     *
     * @param id Id
     * @return Result
     */
    @PreAuth
    @GetMapping("/get")
    @ApiOperation(value = "信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true, value = "ID", paramType = "form"),
    })
    public Result<?> get(@RequestParam String id) {
        return Result.data(flwFormService.getById(id));
    }

    /**
     * 流程表单配置表设置
     *
     * @param flwform flwForm 对象
     * @return Result
     */
    @PreAuth
    @PostMapping("/set")
    @ApiOperation(value = "设置")
    public Result<?> set(@Valid @RequestBody FlwForm flwform) {
        return Result.condition(flwFormService.saveOrUpdate(flwform));
    }

    /**
     * 流程表单配置表删除
     *
     * @param ids id字符串，根据,号分隔
     * @return Result
     */
    @PreAuth
    @PostMapping("/del")
    @ApiOperation(value = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", required = true, value = "多个用,号隔开", paramType = "form")
    })
    public Result<?> del(@RequestParam String ids) {
        return Result.condition(flwFormService.removeByIds($.toLongList(ids)));
    }

    @PreAuth
    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public Result<?> list(FlwFormDTO flwFormDTO) {
        List<FlwForm> list = flwFormService.lambdaQuery().eq(FlwForm::getProcessDefinitionId, flwFormDTO.getProcessDefinitionId()).list();
        List<FlwFormVO> collect = list.stream().map(flwForm -> {
            FlwFormVO flwFormVO = $.copy(flwForm, FlwFormVO.class);
            FlwFormResourceDTO flwFormResourceDTO = new FlwFormResourceDTO();
            flwFormResourceDTO.setId(flwFormVO.getFormId().toString());
            flwFormVO.setFlowableFormResource(flwFormResourceService.detail(flwFormResourceDTO));
            return flwFormVO;
        }).collect(Collectors.toList());
        return Result.data(collect);
    }

    @PreAuth
    @GetMapping("/start-form-data")
    @ApiOperation(value = "启动表单")
    public Result<?> startFormData(FlwFormDTO flwFormDTO) {
        return Result.data(flwFormService.startFormData(flwFormDTO));
    }

    @PreAuth
    @GetMapping("/global-form-data")
    @ApiOperation(value = "全局表单")
    public Result<?> globalFormData(FlwFormDTO flwFormDTO) {
        return Result.data(flwFormService.getGlobalFormData(flwFormDTO));
    }

    @PreAuth
    @GetMapping("/task-form-data")
    @ApiOperation(value = "任务表单")
    public Result<?> taskFormData(FlwFormDTO flwFormDTO) {
        return Result.data(flwFormService.getTaskFormData(flwFormDTO));
    }
}
